package cards;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 * @class Deck represents playing deck and serves also as a drawing pile
 *  
 * @author vlada
 */
public class Deck {
    
    private final ArrayList<Card> cards;

    /**
     * Default constructor
     */
    public Deck() {
        this.cards = new ArrayList<>(Suit.values().length * Rank.values().length);
        for (Suit s : Suit.values()) {
            for (Rank r : Rank.values()) {
                cards.add(new Card(s, r));
            }
        }       
    }
    
    /**
     * Shuffles the deck of cards
     */
    public void shuffle() {
        Collections.shuffle(cards);
    }
    
    /**
     * Removes the first (uppermost) card from the deck
     * @return the removed card
     * @throws IndexOutOfBoundsException if the deck is empty
     */
    public Card removeTop() throws IndexOutOfBoundsException {
        return cards.remove(0);
    }
    
    /**
     * Retrieves (but not removes) the last (bottom) card from the deck
     * @return the bottom card
     * @throws IndexOutOfBoundsException if the deck is empty
     */
    public Card getLast() throws IndexOutOfBoundsException {
        return cards.get(cards.size() - 1);
    }
    
    /**
     * Adds the specified collection of cards to the bottom of the deck
     *  The cards are added in the order that they are
     *  return by the specified collection's Iterator.
     * 
     * @param c collection of the cards to be added.
     * @return true if the content of the deck was changed. 
     * @throws NullPointerException if the specified collection is null
     */
    public boolean addCollection(Collection<? extends Card> c) throws NullPointerException {
        return cards.addAll(c);                
    }
    
    /**
     * Returns true if there are no more cards left in the deck.
     * @return true if there are no more cards left in the deck.
     */
    public boolean isEmpty() {
        return cards.isEmpty();
    }
    
    /**
     * Prints all cards
     * @return 
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for(Card c : cards) {
            sb.append(c.toString());
            sb.append("\n");
        }        
        return sb.toString();
    }
    
}
